package msr.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Logger;

/**
 * クラス：　データベースコネクション管理。 Singleton　を使っている。
 * */

public class ConnectDb {
	private static ConnectDb instance;
	private static int checkout = 0;
	private static final Logger log = Logger.getLogger(ConnectDb.class
			.getName());
	private Connection connection;

	private ConnectDb() {
	}

	public static ConnectDb getInstance() {
		synchronized (ConnectDb.class) {
			if (instance == null) {
				instance = new ConnectDb();
			}
		}
		synchronized (instance) {
			instance.checkout++;
		}
		return instance;
	}

	public static int getCheckout() {
		return checkout;
	}

	public Connection connectDatabase() {
		try {
			Class.forName(Config.CONFIG_ORACLE);
		} catch (ClassNotFoundException e) {
			log.info("error " + e.getMessage());
			return null;
		}
		try {
			instance.connection = DriverManager.getConnection(
					Config.CONFIG_SERVICE, Config.CONFIG_USERNAME,
					Config.CONFIG_PASSWD);
		} catch (Exception ex) {
			log.info("error connecttion" + ex.getMessage());
			return null;
		}
		if (instance.connection != null) {
			return instance.connection;
		} else
			log.info("connect fault!");
		return null;
	}

}
